package com.itwen.utils;

import com.itwen.entity.User;
import com.itwen.mapper.UserMapper;
import com.itwen.web.MyController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: demo
 * @author: Li Wenwen
 * @create: 2019-02-01 11:33
 */
@Configuration
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MyController myController;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = new User();
        user.setUsername(username);
        User user1 = userMapper.selectOne(user);
        if (user1 == null) {
            try {
                throw new Exception("用户名不存在,请重新尝试");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return new org.springframework.security.core.userdetails.User(user1.getUsername(),user1.getPassword(),AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
